﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;

public partial class Admin_NCC : System.Web.UI.Page
{
    DADataContext da = new DADataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            {
                Response.Redirect("/Online/Dangnhap.aspx");
            }
            else
            {
                var mcn = (from cn in da.Chucnangs where cn.Tenchucnang == "Quản lý nhà cung cấp" select cn.Machucnang).FirstOrDefault();
                var kt = (from a in da.TaikhoanChucnangs where (a.Taikhoan == Session["user"].ToString() && a.Machucnang == mcn) select a).Count();
                if (kt == 1)
                {
                    LoadGV();
                    if (string.IsNullOrEmpty(Convert.ToString(Request.QueryString["m"])))
                    {
                        btnThem.Enabled = true;
                        btnSua.Enabled = false;
                    }
                    else
                    {
                        btnSua.Enabled = true;
                        btnThem.Enabled = false;
                        int ma = int.Parse(Request.QueryString["m"]);
                        var load = (from x in da.NCCs where x.MaNCC == ma select x).FirstOrDefault();
                        txtten.Text = load.TenNCC;
                        txtdc.Text = load.Diachi;
                        txtemail.Text = load.Email;
                        txtsdt.Text = load.Sodt;
                    }
                }
                else
                {
                    Response.Redirect("/Online/Admin/Default.aspx");
                }
            }
        }
    }

    private void LoadGV()
    {
        gvNCC.DataSource = from a in da.NCCs select a;
        gvNCC.DataBind();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        if (txtten.Text == "" || txtdc.Text == "" || txtemail.Text == "")
        {
            tb.Text = "Nhập đủ thông tin ";
        }
        else
        {
            NCC n = new NCC();
            n.TenNCC = txtten.Text.Trim();
            n.Diachi = txtdc.Text.Trim();
            n.Email = txtemail.Text.Trim();
            n.Sodt = txtsdt.Text.Trim();
            da.NCCs.InsertOnSubmit(n);
            da.SubmitChanges();
            LoadGV();
            Reset();
            tb.Text = "Thêm thành công !";
        }
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        if (txtten.Text == "" || txtdc.Text == "" || txtemail.Text == "")
        {
            tb.Text = "Nhập đủ thông tin ";
        }
        else
        {
            int m = int.Parse(Request.QueryString["m"]);
            var l = (from a in da.NCCs where a.MaNCC == m select a).FirstOrDefault();
            l.TenNCC = txtten.Text.Trim();
            l.Diachi = txtdc.Text.Trim();
            l.Email = txtemail.Text.Trim();
            l.Sodt = txtsdt.Text.Trim();
            da.SubmitChanges();
            LoadGV();
            Reset();
            btnSua.Enabled = false;
            btnThem.Enabled = true;
            tb.Text = "Cập nhật thành công !";
        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        StringCollection idConnllection = new StringCollection();
        string strids = string.Empty;

        for (int i = 0; i < gvNCC.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)gvNCC.Rows[i].Cells[0].FindControl("ckq1");
            if (ck != null)
            {
                if (ck.Checked == true)
                {
                    Label lb = (Label)gvNCC.Rows[i].Cells[1].FindControl("Ma");
                    strids = lb.Text;
                    idConnllection.Add(strids);
                }
            }
        }
        if (idConnllection.Count > 0)
        {
            string iDS = ""; int a;
            foreach (string id in idConnllection)
            {
                iDS += id + ",";
            }
            string[] mang = iDS.Split(',');
            for (int i = 0; i < mang.Length - 1; i++)
            {
                a = int.Parse(mang[i]);
                var xoa = da.NCCs.SingleOrDefault(p => p.MaNCC == a);
                if (xoa != null)
                {
                    da.NCCs.DeleteOnSubmit(xoa);
                    da.SubmitChanges();
                }
            }
            tb.Text = "Xóa thành công!";
            LoadGV();
            Reset();
        }
        else
            tb.Text = "Không bản ghi nào được chọn để xóa!";
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Online/Admin/NCC.aspx");
    }
    void Reset()
    {
        txtten.Text = "";
        txtdc.Text = "";
        txtemail.Text = "";
        txtsdt.Text = "";
        txtten.Focus();
    }
}
